package com.polaris.live.widget.room_banner

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.adapter.RoomBannerAbsAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.WidgetRoomAbsBannerBinding
import com.polaris.live.resp.back_resp.RoomBannerItemResp
import com.zhpan.indicator.enums.IndicatorSlideMode

/**
 * LiveAbsBannerView  直播广告轮播图
 *
 * @author Created by 半仙 on 2023/11/11/011 9:59
 */
class RoomAbsBannerView(context: Context, attributeSet: AttributeSet?) : LinearLayout(context, attributeSet) {

    private val mBinding = WidgetRoomAbsBannerBinding.inflate(LayoutInflater.from(context), this, true)

    private val mBannerAdapter by lazy {
        RoomBannerAbsAdapter(findViewTreeLifecycleOwner()?.lifecycleScope)
    }

    fun setAbsBanner(lifecycle: Lifecycle, banners: List<RoomBannerItemResp>) {
        mBinding.bannerView.apply {
            if (banners.size == 1) {
                mBinding.indicatorView.visibility = View.GONE
            } else {
                mBinding.indicatorView.visibility = View.VISIBLE
            }
            registerLifecycleObserver(lifecycle)
            setAdapter(mBannerAdapter)
            setIndicatorVisibility(View.GONE)
            setIndicatorSlideMode(IndicatorSlideMode.SMOOTH)
            setIndicatorView(mBinding.indicatorView)
            setIndicatorSliderColor(
                ColorUtils.getColor(R.color.color_ffffff_30),
                ColorUtils.getColor(R.color.white)
            )
            setIndicatorSliderWidth(4.dp)
            setScrollDuration(500)
            create(banners)
        }
    }
}